import javafx.stage.Stage;
import javafx.stage.Alert.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.control.Slider;
import javafx.scene.media.*;
import javax.swing.JFileChooser;

/**
 * @author Besitzer
 */

var stage:Stage;
var video:HBox;
var play_timeline:HBox;
var play : Button;
var timeline : ProgressBar;
var medium : Media;
var player : MediaPlayer;
var hell : HBox;
var helligkeit : Slider;
var effekt : HBox;
var Staerke : HBox;
var frames : Slider;
var open_save : HBox;

var file_path: String;
var filechooser: JFileChooser = new JFileChooser();

var title : String = "CB-Movie Player fuer Ruckelnde Bilder";
var info_name: Label;
var info_description: Label;
stage = Stage
{
  title: title;
  width:  600;
  height: 700;
  resizable: false;
  opacity: 1; //TODO: zwischen 1 und 0

  	scene: Scene
	{

	    content:
	    [
	    	video = HBox
	    	{

	    	        translateX: 0;
	    	        translateY: 0;
	    	        height: 450;
	    	        width:	bind stage.width;


	    	        content: MediaView
	    	        {
	    	            translateX: 5;
	    	            translateY: 5;
	    	            fitWidth : bind video.width -15;
	    	            fitHeight: bind video.height - 10;
                            onError: function(e:MediaError) {
                                                    inform(e.toString());
                                            }

	    	            mediaPlayer: player = MediaPlayer
	    	            {
                                    autoPlay: false;
                                    repeatCount: player.REPEAT_FOREVER;
                                    onError: function(e:MediaError) {
                                                    inform(e.toString());
                                            }
                                    media: medium = Media
                                    {
                                           onError: function(e:MediaError) {
                                                    inform(e.toString());
                                            }
                                            source: bind file_path;
                                    }

                                    onEndOfMedia: function():Void
                                    {
                                     timeline.progress = 0;
                                    }
	    	        	}

	    	        }
	   		}
	   	/*Line {
                                            startX: 0;
                                            startY: 450;
                                            endX: bind stage.width-1;
                                            endY: 450;
                                            strokeWidth: 1;
                                            stroke: Color.DARKGRAY;
                                    }*/
	   	play_timeline = HBox
	   	{
	   		    translateX: 0;
	   		    translateY: bind video.height+5;
	   		    height: 40;
	   		    width: bind stage.width;
	   		    content:
	   		    [



	   			    play = Button
	   			    {


	   			        translateX: 10;
	   			        translateY: 5;
                                        //TODO: groesse von button
                                        text: "Play";
	   			       	action: function(): Void
                                        {
                                            if(medium.source == "")
                                            {
                                                inform("Media Failure","Please insert Media");
                                            }
                                            else
                                            {
                                                if(player.paused)
                                                {

                                                        player.play();
                                                        play.text = "Pause";

                                                } else if(player.paused == false)
                                                {

                                                    player.pause();
                                                    play.text = "Play";

                                                } else
                                                {
                                                    //gestoppt
                                                }
                                            }
	   			   	 }
	   			    }
                                    timeline = ProgressBar {

                                            progress: bind ProgressBar.computeProgress( medium.duration.toSeconds(), player.currentTime.toSeconds())
                                            translateX: 25;
                                            translateY: 10;

                                            width:  bind stage.width -200;

                                    }


	   			    Label
	   			    {

                                        translateX: 40;
	   			       	translateY: 10;
	   			      	text: bind "{player.currentTime.toMinutes()}:{player.currentTime.toSeconds()}"
                                        //TODO: Double to Int
                                    }

	   			]

	   		}

                hell = HBox
                {
                    translateX: 0;
                    translateY: bind video.height+play_timeline.height;
                    height: 30;
                    width: bind stage.width;
                    content:
                    [

                        Label
                        {
                            translateX: 15;
                            translateY: 5;
                            text: "Helligkeit: ";
                        }
                        helligkeit = Slider
                        {
                             translateX: 25;
                             translateY: 5;
                             width: stage.width -200;

                             min: 0;
                             max: 100;

                             vertical: false

                        }
                        Label
                        {
                            translateX: 50;
                            translateY: 5;
                           // helligkeit.
                            text: bind "{helligkeit.value} %";
                        }

                   ]

                }
                effekt = HBox
                {
                    translateX: 0;
                    translateY: bind video.height+play_timeline.height+hell.height;
                    height: 30;
                    width: bind stage.width;
                    content:
                    [
                           CheckBox
                           {
                                    translateX: 15;
                                    translateY: 5;
                                    text: " Ruckelende Bilder";
                                    allowTriState: false;
                                    selected: false;
                           }
                           TextBox
                           {
                                   translateX: 30;
                                   text: ""
                                   columns: 2
                           }
                           Label
                           {
                                translateX: 40;
                                translateY: 5;
                               text: "Dauer(s)";
                           }
                           TextBox
                           {
                                   translateX: 60;
                                   text: ""
                                   columns: 2
                           }
                           Label
                           {
                                   translateX: 70;
                                   translateY: 5;
                               text: "Beginn(s)";
                           }

                    ]
                }
                Staerke = HBox
                {
                    translateX: 0;
                    translateY: bind video.height+play_timeline.height+hell.height+ effekt.height;
                    height: 30;
                    width: bind stage.width;
                    content:
                    [
                        Label
                        {
                            translateX: 15;
                            translateY: 5;
                            text: "Staerke:    ";
                        }
                        frames = Slider
                        {
                             translateX: 25;
                             translateY: 5;
                             width: stage.width -200;

                             min: 0;
                             max: 5;

                             vertical: false

                        }
                        Label
                        {
                            translateX: 50;
                            translateY: 5;
                           // helligkeit.
                            text: bind "{frames.value}";
                        }
                        CheckBox
                        {
                                translateX: 80;
                                translateY: 5;
                                text: "zufaellige Auswahl"
                                allowTriState: false
                                selected: false
                        }

                    ]
                }
                open_save = HBox
                {
                    translateX: 0;
                    translateY: bind video.height+play_timeline.height+hell.height+ effekt.height + Staerke.height;
                    height: 30;
                    width: bind stage.width;
                    content:
                    [
                                Button
                                {
                                    translateX: 10;
                                    text: "Open";
                                    action: function()
                                    {
                                        filechooser.setDialogTitle("Open Media-File");
                                        if(JFileChooser.APPROVE_OPTION == filechooser.showOpenDialog(null))
                                        {
                                            //player.stop();
                                            var file_stream = filechooser.getSelectedFile();
            
                                            file_path = file_stream.toURI().toString();
                                            stage.title = "{ title } now playing: {filechooser.getName(file_stream)}";
                                            info_name.text = "Now Playing: {filechooser.getName(file_stream)}";
                                            info_description.text = "Hoehe: {medium.height}  Breite: {medium.width}";
                                        }
                                        else
                                        {
                                            stage.title = "fail to open file";//TODO......Fehlerbehandlung.......///
                                        }
                                        /*Returns:
                                            the return state of the file chooser on popdown:
                                            JFileChooser.CANCEL_OPTION
                                            JFileChooser.APPROVE_OPTION
                                            JFileCHooser.ERROR_OPTION if an error occurs or the dialog is dismissed*/

                                        //filechooser.setFileFilter(FileFilter filter)
                                        //player.play();
                                     }

                                }
                                Button
                                {
                                    translateX: 20;
                                    text: "Save";
                                    action: function()
                                    {

                                    }
                                }
                    ]
                }
                HBox
                {
                    translateX: 0;
                    translateY: bind video.height+play_timeline.height+hell.height+ effekt.height + Staerke.height+ open_save.height;
                    height: 30;
                    width: bind stage.width;
                    content:
                    [
                        info_name = Label
                        {
                            translateX: 10;
                            translateY: 5;
                           // helligkeit.
                            text: "";
                        }
                        info_description = Label
                        {
                            translateX: 80;
                            translateY: 5;
                            text: "";
                        }
                   ]
                }
             ]
	}
}